package com.couchbase.cityexplorer; import android.app.Activity; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.widget.TextView; import com.couchbase.lite.CouchbaseLiteException; import com.couchbase.lite.Document; import com.couchbase.lite.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { public static final String TAG = "CityExplorer"; GoogleApiClient mGoogleApiClient; TextView cityLabel; TextView longitudeLabel; SyncManager syncManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cityLabel = (TextView) findViewById(R.id.latitudeLabel); longitudeLabel = (TextView) findViewById(R.id.longitudeLabel); syncManager = new SyncManager(this); buildGoogleApiClient(); } protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); mGoogleApiClient.connect(); } @Override public void onConnected(Bundle bundle) { Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (mLastLocation != null) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); } if (addresses != null && addresses.size() > 0) { String city = addresses.get(0).getLocality(); cityLabel.setText(city); Document document = syncManager.getDatabase().getDocument("james"); Map<String, Object> properties = new HashMap<>(); if (document.getProperties() != null) { properties.putAll(document.getProperties()); } properties.put("type", "profile"); properties.put("city", city); try { document.putProperties(properties); Log.d(TAG, "Saved document with properties :: %s", document.getProperties().toString()); } catch (CouchbaseLiteException e) { e.printStackTrace(); } } } } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } }